/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlers;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.Customer;
import model.ProductPurchase;
import model.Purchase;

/**
 *
 * @author Tasos 
 * o Buy Controler parexei yphresies kata thn diadikasia agoras
 * Einai ena symplirwma toy Control Level toy BuyProducts Screen
 */
public class BuyControler {
private static EntityManager em5; 
    public BuyControler() {
    //dhmiourgia entity manager
     EntityManagerFactory emf = Persistence.createEntityManagerFactory("SuperMarketPU");
     em5 = emf.createEntityManager();         
    }

    
    //prosthetei sthn vash mia synolikh agora p
    public void addPurchase(Purchase p){
    em5.getTransaction().begin();
    em5.persist(p);
    em5.getTransaction().commit();
    }

    //prosthetei sthn vash mia agora proiontos pp
    public void addProductPurchase(ProductPurchase pp){
    em5.getTransaction().begin();
    em5.persist(pp);
    em5.getTransaction().commit();    
    }

    //enhmerwnei thn vash me toys pontous pou kerdise enas
    //pelaths apo mia agora
    public void addPoints(Customer cust,int points){
    em5.getTransaction().begin();
    cust.setAvailablePoints(cust.getAvailablePoints() + points);
    em5.merge(cust);
    em5.getTransaction().commit();    
    }

}
